package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author shiminghui
 * @date 2024/12/18 16:35
 * @description: TODO
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Long id) {
        WmSensitive adSensitive = wmSensitiveMapper.selectById(id);
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        int i = wmSensitiveMapper.deleteById(id);
        if (i > 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 敏感词列表
     *
     * @param adSensitiveDto
     * @return
     */
    @Override
    public ResponseResult list(WmSensitiveDto adSensitiveDto) {
        if (adSensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adSensitiveDto.checkParam();

        Page<WmSensitive> page = new Page<>(adSensitiveDto.getPage(), adSensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = null;
        if (adSensitiveDto.getName() != null && !adSensitiveDto.getName().isEmpty()) {
            queryWrapper = Wrappers.lambdaQuery(WmSensitive.class).like(WmSensitive::getSensitives, adSensitiveDto.getName());
        }

        Page<WmSensitive> resultPage = wmSensitiveMapper.selectPage(page, queryWrapper);
        PageResponseResult result = new PageResponseResult();
        result.setData(resultPage.getRecords());
        result.setTotal((int) resultPage.getTotal());
        result.setCurrentPage(adSensitiveDto.getPage());
        result.setSize(adSensitiveDto.getSize());


        return result;
    }

    /**
     * 保存敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveWmSensitive(WmSensitive adSensitive) {

        WmSensitive sensitive = wmSensitiveMapper.selectOne(Wrappers.lambdaQuery(WmSensitive.class).eq(WmSensitive::getSensitives, adSensitive.getSensitives()));
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }

        adSensitive.setCreatedTime(new Date());
        int i = wmSensitiveMapper.insert(adSensitive);
        if (i > 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 修改
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(WmSensitive adSensitive) {
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive sensitive = wmSensitiveMapper.selectById(adSensitive.getId());
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        sensitive.setSensitives(adSensitive.getSensitives());
        int i = wmSensitiveMapper.updateById(sensitive);
        if (i > 0) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
